package com.acme.bankapp.domain.bank;

import java.io.Serializable;
import java.util.Arrays;

public class Client {

    private static int numOfClients = 0;

    private Account accounts[];
    private int numOfAccounts = 0;
    private String name;
    private Gender gender;
    private int clientID;

    public Client(String name, Gender gender, Account c_account, Account s_account) {
        this.name = name;
        numOfClients++;
        clientID = numOfClients;
        this.gender = gender;
        accounts = new Account[2];
        addAccount(c_account);
        addAccount(s_account);
    }

    public Client(String name, Gender gender) {
        this.name = name;
        this.gender = gender;
        accounts = new Account[2];
    }

    public String getName() {
        return name;
    }

    public Gender getGender() {
        return gender;
    }

    public int getClientID() {
        return clientID;
    }

    public Account[] getAccounts() {
        return accounts;
    }

    public void addAccount(Account account) {
        accounts[numOfAccounts] = account;
        account.setAccountID(numOfAccounts);
        numOfAccounts++;
    }

    public enum Gender {
        MALE("Mr."), FEMALE("Ms.");

        private String salutation;
        private Gender (String salutation) {
            this.salutation = salutation;
        }

        public String getSalutation () {
            return this.salutation;
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Client client = (Client) o;

        if (clientID != client.clientID) return false;
        if (numOfAccounts != client.numOfAccounts) return false;
        if (!Arrays.equals(accounts, client.accounts)) return false;
        if (gender != client.gender) return false;
        if (name != null ? !name.equals(client.name) : client.name != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = accounts != null ? Arrays.hashCode(accounts) : 0;
        result = 31 * result + numOfAccounts;
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + (gender != null ? gender.hashCode() : 0);
        result = 31 * result + clientID;
        return result;
    }

    @Override
    public String toString() {
        return new StringBuffer()
                .append("Client{")
                .append("accounts=")
                .append(Arrays.toString(accounts))
                .append(", numOfAccounts=")
                .append(numOfAccounts)
                .append(", name='")
                .append(name)
                .append('\'')
                .append(", gender=")
                .append(gender)
                .append(", clientID=")
                .append(clientID)
                .append('}')
                .toString();
    }
}
